﻿using Treca.Base_SysManage.Entity;
using Treca.Business.Base_SysManage;
using Treca.Util;
using Microsoft.Extensions.Options;
using NPOI.SS.Formula.Functions;
using Senparc.NeuChar;
using Senparc.NeuChar.Context;
using Senparc.NeuChar.Entities;
using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.MessageContexts;
using Senparc.Weixin.MP.MessageHandlers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace Treca.Web.Areas.Base_SysManage.Controllers
{
    public class CustomMessageHandler : MessageHandler<DefaultMpMessageContext>
    {
        public PostModel _postModel { get; set; }
        public CustomMessageHandler(XDocument inputStream, PostModel postModel)
           : base(inputStream, postModel)
        {
            _postModel = postModel;
        }

        public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
        {
            switch (requestMessage.MsgType)
            {
                case RequestMsgType.Text:
                    var ResponseMessage = CreateResponseMessage<ResponseMessageNews>();
                    ResponseMessage.Articles.Add(new Article()
                    {
                        Title = "账号绑定",
                        Description = "账号绑定",
                        PicUrl = "http://www.rdiframework.net/img/weixing-ma.png",
                        Url = GetAuthorize("https://www.huangjianjian.com/PushMessage/ms_messageservice/Login")
                    });
                    return ResponseMessage;
                default:
                    //这个过程实际已经在OnTextOrEventRequest中完成，这里不会执行到。
                    var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
                    strongResponseMessage.Content = "您点击了底部按钮。\r\n为了测试微信软件换行bug的应对措施，这里做了一个——\r\n换行";
                    return strongResponseMessage;
            }
        }
        IBase_UserBusiness userBus { get => AutofacHelper.GetService<IBase_UserBusiness>(); }
        public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
        {
            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            IResponseMessageBase reponseMessage = null;
            //菜单点击，需要跟创建菜单时的Key匹配
            switch (requestMessage.EventKey)
            {
                case "Face":
                    {
                        var ResponseMessage = CreateResponseMessage<ResponseMessageNews>();
                        ResponseMessage.Articles.Add(new Article()
                        {
                            Title = "账号绑定",
                            Description = "账号绑定",
                            PicUrl = "http://www.rdiframework.net/img/weixing-ma.png",
                            Url = GetAuthorize("https://www.huangjianjian.com/PushMessage/ms_messageservice/Login")
                        });
                        reponseMessage = ResponseMessage;
                        return reponseMessage;
                    }
                case "FaceList":
                    {
                        var ResponseMessage = CreateResponseMessage<ResponseMessageNews>();
                        ResponseMessage.Articles.Add(new Article()
                        {
                            Title = "预约列表",
                            Description = "预约列表",
                            PicUrl = "http://www.rdiframework.net/img/weixing-ma.png",
                            Url = GetAuthorize("http://weixin.gooderp.net/Monitor/Face/IndexByUser")
                        });
                        reponseMessage = ResponseMessage;
                        return reponseMessage;
                    }
            }
            strongResponseMessage.Content = "您点击了底部按钮。\r\n为了测试微信软件换行bug的应对措施，这里做了一个——\r\n换行";
            reponseMessage = strongResponseMessage;
            return reponseMessage;
        }
        public static IOptions<SenparcWeixinSetting> senparcWeixinSetting { get => AutofacHelper.GetService<IOptions<SenparcWeixinSetting>>(); }
        /// <summary>
        /// 通过授权跳转页面
        /// </summary>
        /// <param name="url"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public  string GetAuthorize(string url)
        {
            var state = Guid.NewGuid().ToString().Replace("-", "").ToLower();
            var urlencode = WebUtility.HtmlEncode(url);
            return string.Format(@"https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state={2}#wechat_redirect"
                                , _postModel.AppId, urlencode, _postModel.AppId);
        }
    }
}
